package com.ssyt.ec.sys.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ssyt.ec.common.AppServlet;
import com.ssyt.ec.common.util.FastjsonUtil;
import com.ssyt.ec.common.util.SysuserUtil;
import com.ssyt.ec.sys.entity.MenuEntity;
import com.ssyt.ec.sys.entity.SysUserEntity;
import com.ssyt.ec.sys.service.SysUserService;

@WebServlet("/test/TestServlet")
public class TestServlet extends AppServlet {

	private static final long serialVersionUID = 1L;
	SysUserService sysUserService = new SysUserService();

	public TestServlet() {
		super();
	}

	@Override
	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String command = request.getParameter("command");
		if ("listMenu".equals(command)) {
			listMenu(request, response);
		} else if ("login".equals(command)) {
			login(request, response);
		} else if ("logout".equals(command)) {
			logout(request, response);
		} else if ("updatePwd".equals(command)) {
			updatePwd(request, response);
		} else if ("updateUser".equals(command)) {
			updateUser(request, response);
		}
	}

	private void listMenu(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		int id = 0;
		String idStr = request.getParameter("id");
		if (idStr != null) {
			id = Integer.parseInt(idStr);
		}
		SysUserService sysUserService = new SysUserService();
		List<MenuEntity> menuList = sysUserService.listMenu(id,
				SysuserUtil.getUserId(request));
		print(response, FastjsonUtil.toJson(menuList));
		// List<MenuEntity> menuList = DBUtil.executeQuery(MenuEntity.class,
		// "SELECT * FROM sys_menu WHERE pid=?", id);
		// print(response, FastjsonUtil.toJson(menuList));
	}

	/**
	 * 登录功能
	 * 
	 * @param request
	 * @param response
	 */
	private void login(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// String validateCode = request.getParameter("validateCode");
		// String vcode = (String) request.getSession().getAttribute(
		// com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
		// if (vcode.equals(validateCode)) {
		String code = request.getParameter("code");
		String password = request.getParameter("password");
		SysUserEntity sysUser = sysUserService.login(code, password);
		if (sysUser != null) {
			request.getSession().setAttribute("sysUser", sysUser);
			print(response, 1);
		} else {
			print(response, 2);
		}
		// } else {
		// request.setAttribute("msg", "验证码输入错误！");
		// print(response, "msg");
		// }
	}

	/**
	 * 注销
	 * 
	 * @param request
	 * @param response
	 */
	private void logout(HttpServletRequest request, HttpServletResponse response) {
		// 注销并移除session
		request.getSession().invalidate();
		try {
			response.sendRedirect("login.jsp");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 修改信息
	 * 
	 * @param request
	 * @param response
	 */

	private void updateUser(HttpServletRequest request,
			HttpServletResponse response) {
		String email = request.getParameter("email");
		String phone = request.getParameter("phone");
		List<SysUserEntity> userList = sysUserService.getUniqueUser(email,
				phone);
		String data = "";
		if (userList.size() <= 1) {
			sysUserService.updateUser(email, phone,
					SysuserUtil.getUserId(request));
			data = "更新成功!";
		} else {
			data = "这邮箱或者手机号 已经注册过 操作失败";
		}
		print(response, data);
	}

	/**
	 * 修改密码
	 */
	private void updatePwd(HttpServletRequest request,
			HttpServletResponse response) {
		String newpwd = request.getParameter("newpwd");
		String repwd = request.getParameter("repwd");
		if (repwd.equals(newpwd)) {
			int row = sysUserService.updatePwd(newpwd,
					SysuserUtil.getUserId(request));
			if (row != 0) {
				print(response, 1);
				SysUserEntity User = (SysUserEntity) request.getSession()
						.getAttribute("sysUser");
				// 修改后的 session
				User.setPassword(newpwd);
				request.getSession().setAttribute("sysUser", User);
			} else {
				print(response, 2);
			}
		} else {
			print(response, 2);
		}
	}
}
